package com.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.entity.Parking;
import com.community.entity.ParkingPlace;
import com.community.service.ParkingPlaceService;
import com.community.service.ParkingService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * (ParkingPlace)表控制层
 *
 * @author makejava
 * @since 2021-11-10 11:46:11
 */
@Controller
@RequestMapping("parkingPlace")
public class ParkingPlaceController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ParkingPlaceService parkingPlaceService;
    @Resource
    ParkingService parkingService;

    /**
     * 前往车辆管理页面
     */
    @RequestMapping("parkingPlace_add")
    public String toParkingPlaceadd() {
        return "parkingPlace_add";
    }

    /**
     * 前往车辆管理页面
     */
    @RequestMapping("parkingPlace_list")
    public String toParkingPlace() {
        return "parkingPlace_list";
    }

    /**
     * 分页查询所有数据
     *
     * @param page         分页对象
     * @param ParkingPlace 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    @ResponseBody
    public R selectAll(Page<ParkingPlace> page, ParkingPlace ParkingPlace) {
        QueryWrapper<ParkingPlace> wrapper = new QueryWrapper<>();
        wrapper.like(ParkingPlace.getOwner() != null, "owner", ParkingPlace.getOwner());
        wrapper.between(ParkingPlace.getBeginDate() != null && ParkingPlace.getOverDate() != null,
                "create_date", ParkingPlace.getBeginDate(), ParkingPlace.getOverDate());
        return success(this.parkingPlaceService.page(page, wrapper));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ResponseBody
    public R selectOne(@PathVariable Serializable id) {
        return success(this.parkingPlaceService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param ParkingPlace 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    @ResponseBody
    public R insert(ParkingPlace ParkingPlace) {
        ParkingPlace.setCreateDate(new Date());
        
        Parking parking = new Parking();
        parking.setPersonnelState(2);
        parkingService.update(parking, new QueryWrapper<Parking>().eq("stall", ParkingPlace.getParking()));
        return success(this.parkingPlaceService.save(ParkingPlace));
    }

    /**
     * 修改数据
     *
     * @param ParkingPlace 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    @ResponseBody
    public R update(ParkingPlace ParkingPlace) {
        return success(this.parkingPlaceService.updateById(ParkingPlace));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    @ResponseBody
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.parkingPlaceService.removeByIds(idList));
    }
}

